﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SSMS
{
    public class XModeSnd
    {
        /// <summary>
        /// 要访问的dll
        /// </summary>
        public String File = "";
        /// <summary>
        /// 要访问的方法
        /// </summary>
        public String Method = "";
        /// <summary>
        /// 登录用的ID
        /// </summary>
        public String UID = "";
        /// <summary>
        /// 被序列化成json的字符串
        /// </summary>
        public String JsonObject = "";
        /// <summary>
        /// 被序列化成json的类型
        /// </summary>
        public String ObjectType = "";

        public object this[String key]
        {
            get
            {
                key = key.ToLower();
                if (!Data.ContainsKey(key))
                {
                    throw new Exception(String.Format("字典中不包含{0}", key));
                }
                else
                {
                    return Data[key];
                }
            }
            set
            {
                key = key.ToLower();
                Data[key] = value;
            }
        }

        /// <summary>
        /// 字典数据
        /// </summary>
        public DataItem Data = new DataItem();
        /// <summary>
        /// 数据集字典数据
        /// </summary>
        public DataList List = new DataList();
    }

    public class XModeRtn
    {
        /// <summary>
        /// 执行状态 正常 错误 重新登录
        /// </summary>
        public XModeState State = XModeState.Normal;
        /// <summary>
        /// 返回消息
        /// </summary>
        public String Message = "";
        /// <summary>
        /// 被序列化成json的字符串
        /// </summary>
        public String JsonObject = "";
        /// <summary>
        /// 被序列化成json的类型
        /// </summary>
        public String ObjectType = "";
        /// <summary>
        /// 字典数据
        /// </summary>
        public DataItem Data = new DataItem();
        /// <summary>
        /// 数据集字典数据
        /// </summary>
        public DataList List = new DataList();

        public object this[String key]
        {
            get
            {
                key = key.ToLower();
                if (!Data.ContainsKey(key))
                {
                    throw new Exception(String.Format("字典中不包含{0}", key));
                }
                else
                {
                    return Data[key];
                }
            }
            set
            {
                key = key.ToLower();
                Data[key] = value;
            }
        }
    }

    public enum XModeState : int
    {
        Normal = 0,
        Error = 1,
        ReLoad = 2,
        NoPermission = 3
    }

    public class DataList
    {
        public List<DataItem> List = new List<DataItem>();
        public int Count { get { return List.Count; } }
        public DataItem this[int index]
        {
            get
            {
                if (List.Count - 1 < index)
                {
                    throw new Exception(String.Format("索引越界{0}", index));
                }

                return List[index];
            }
        }

        public void Clear()
        {
            List.Clear();
        }
        public void Add(DataItem item)
        {
            List.Add(item);
        }

    }

    public class DataItem
    {
        public Dictionary<String, object> Data = new Dictionary<String, object>();

        public object this[String key]
        {
            get
            {
                if (!Data.ContainsKey(key))
                {
                    return null;
                }
                else
                {
                    return Data[key];
                }
            }
            set
            {
                Data[key] = value;
            }
        }

        public void Clear()
        {
            Data.Clear();
        }

        public Boolean ContainsKey(string key)
        {
            return Data.ContainsKey(key);
        }

        public Type GetValueType(string key)
        {
            return Data[key].GetType();
        }
    }

    public class DataBook
    {
        public DataItem Items = new DataItem();
        public List<DataItem> List = new List<DataItem>();

        public object this[String key]
        {
            get
            {
                key = key.ToLower();
                if (!Items.ContainsKey(key))
                {
                    throw new Exception(String.Format("字典中不包含{0}", key));
                }
                else
                {
                    return Items[key];
                }
            }
            set
            {
                key = key.ToLower();
                Items[key] = value;
            }
        }
    }

    public class XModeMsg
    {
        public String Note;
        public MessageType Type;
    }
    public class XCodeResult
    {
        public string Content;
        public string Message;
        public EnumResult Result;
        public SerialType SerialType;

        //public XCodeResult();
    }
    public enum EnumResult
    {
        Normal = 0,
        Error = 1,
        ReLoad = 2,
    }
}
